package cn.com.open.itembank.controller;

import cn.com.open.itembank.dto.ChapterInputModel;
import cn.com.open.itembank.dto.ReturnModel;
import cn.com.open.itembank.entity.Chapter;
import cn.com.open.itembank.service.ChapterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Created by lucky on 2017/9/5.
 */
@RestController
@RequestMapping(value = "/itembank")
public class ChapterController {

    @Autowired
    ChapterService service;

    //region  新增章节

    /**
     * 新增章节
     *
     * @param chapterModel
     * @return
     */
    @PostMapping(value = "/{itembankId}/chapter")
    public ReturnModel insert(@PathVariable("itembankId") String itembankId, @RequestBody Chapter chapterModel) {
        return service.insert(itembankId,chapterModel);
    }

    //endregion

    //region 获取章节

    /**
     * 获取课程全部章节
     *
     * @param itembankId itembankId
     * @return 可用题库列表
     */
    @GetMapping(value = "/{itembankId}/chapterlist")
    public ReturnModel getCourseChapter(@RequestParam("appKey") String appKey,
                                        @PathVariable("itembankId") String itembankId) {
        return service.getCourseChapter(appKey, itembankId);
    }

    /**
     * 获取课程题库下所有章节列表(树)
     *
     * @param itembankId itembankId
     * @return 可用题库列表
     */
    @GetMapping(value = "/{itembankId}/chaptertree")
    public ReturnModel getAllChaptList(@RequestParam("appKey") String appKey,
                                       @PathVariable("itembankId") String itembankId) {
        return service.getAllChaptList(appKey, itembankId);
    }

    /**
     * 获取章节（策略用）
     *
     * @param itembankId itembankId
     * @return 可用题库列表
     */
    @GetMapping(value = "/{itembankId}/getchildchapterbyguid")
    public ReturnModel getChaptForRules(@RequestParam("appKey") String appKey,
                                        @PathVariable("itembankId") String itembankId,
                                        @RequestParam("firstChapterId") String firstChapterId,
                                        @RequestParam("lastChapterId") String lastChapterId) {
        return service.getChaptForRules(appKey, itembankId, firstChapterId, lastChapterId);
    }

    /**
     * 获取章节（策略修改用）
     *
     * @param itembankId itembankId
     * @return 可用题库列表
     */
    @GetMapping(value = "/{itembankId}/getcheckchapterbyguid")
    public ReturnModel getChaptForUpdateRules(@RequestParam("appKey") String appKey,
                                              @PathVariable("itembankId") String itembankId,
                                              @PathVariable("chapterId") String chapterId) {
        return service.getChaptForUpdateRules(appKey, itembankId, chapterId);
    }

    /**
     * 获取章节
     *
     * @param chapterId chapterId
     * @return 可用题库列表
     */
    @GetMapping(value = "/{itembankId}/chapter/{chapterId}/getmodel")
    public Chapter getChapterInfo(@PathVariable(value = "chapterId") String chapterId) {
        return service.getChapterById(chapterId);
    }


    /**
     * 获取章节获取(根据章节集合和章节id串)
     *
     * @param itembankId itembankId
     * @param chapter    chapter
     * @return 可用题库列表
     */
    @GetMapping(value = "/{itembankId}/chapter/{chapterId}/getlist")
    public ReturnModel getChildModel(@RequestParam("appKey") String appKey,
                                     @PathVariable("itembankId") String itembankId,
                                     @RequestBody List<Chapter> chapter,
                                     @PathVariable("chapterId") String chapterId) {
        return service.getChildModel(appKey, itembankId, chapter, chapterId);
    }

    //endregion

    //region 修改章节

    /**
     * 修改章节名称.
     *
     * @param chapterId
     * @param chapterName
     * @return
     */
    @PutMapping(value = "/{itembankId}/chapter/{chapterId}")
    public ReturnModel update(@RequestParam("appKey") String appKey,
                      @PathVariable("itembankId") String itembankId,
                        @PathVariable("chapterId") String chapterId,
                      @RequestBody String chapterName) {
        return service.updateChapterName(appKey,itembankId,chapterId, chapterName);
    }

    /**
     * 拖拽更新章节
     *
     * @param appKey
     * @param itembankId
     * @param chapterId
     * @param parentId
     * @param type
     * @return
     */
    @PutMapping(value = "/{itembankId}/chapter/{chapterId}/drag")
    public ReturnModel updateChapter(@RequestParam("appKey") String appKey,
                                     @PathVariable("itembankId") String itembankId,
                                     @PathVariable("chapterId") String chapterId,
                                     @RequestParam("parentId") String parentId,
                                     @RequestParam("type") Integer type) {
        return service.updateChapter(appKey, itembankId, chapterId, parentId, type);
    }

    /**
     * 拖拽后数据同步
     *
     * @param appKey
     * @param itembankId
     * @param chapterInputModel
     * @return
     */
    @PutMapping(value = "/{itembankId}/chapter/dragchapter")
    public ReturnModel dragChapter(@RequestParam("appKey") String appKey,
                                   @PathVariable("itembankId") String itembankId,
                                   @RequestBody ChapterInputModel chapterInputModel) {
        return service.dragChapter(appKey, itembankId, chapterInputModel);
    }
    //endregion

    //region 删除章节

    /**
     * 删除题库下全部章节信息.
     *
     * @param itembankId
     * @return
     */
    @DeleteMapping(value = "/{itembankId}/chapter")
    public ReturnModel deleteByItemBankId(@PathVariable("itembankId") String itembankId) {
        return service.deleteByItemBankId(itembankId);
    }

    /**
     * 根据章节ID删除章节信息
     *
     * @param itembankId
     * @return
     */
    @DeleteMapping(value = "/{itembankId}/chapter/{chapterId}/deletechapter")
    public ReturnModel deleteById(@RequestParam("appKey") String appKey,
                                  @PathVariable("itembankId") String itembankId,
                                  @PathVariable("chapterId") String chapterId) {
        return service.deleteById(appKey, itembankId, chapterId);
    }

    /**
     * 根据子章节ID删除章节信息
     *
     * @param itembankId
     * @return
     */
    @DeleteMapping(value = "/{itembankId}/chapter/{chapterId}")
    public ReturnModel deleteByChildId(@RequestParam("appKey") String appKey,
                                       @PathVariable("itembankId") String itembankId,
                                       @RequestParam("chapters") String chapters,
                                       @PathVariable("chapterId") String chapterId) {
        return service.deleteByChildId(appKey, itembankId, chapters, chapterId);
    }
    //endregion
}
